package com.ctbri.manage.process;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

import com.ctbri.manage.process.model.GeneratorConfig;
import com.ctbri.manage.process.utils.DataProcessUtils;

/**
 * @author qs
 */
public class DataProcessTest {
    private static DataProcessUtils dataProcessUtils = null;

    public static void main(String[] args) {
        String path = DataProcessTest.class.getClassLoader().getResource("").getPath();//注意getResource("")里面是空字符串
        path = path.substring(1);
        String filePath = path +"generatorConfig.properties" ;
        dataProcessUtils = new DataProcessUtils();
       // dataProcessUtils.getDynamicSqlByConfig(filePath);
        dataProcessUtils.getDynamicSqlFromTempFile(filePath);
    }

    /**
     * 通过命令方式进行测试
     **/
    public static void testmain(String[] args) {
        //动态获取配置文件路径
        if (args.length <= 0) {
            System.err.println("USAGE:java -jar data-process-1.0-jar-with-dependencies.jar ./generatorConfig.properties");

        }
        Properties properties = new Properties();
//        try {
//            properties.load(new InputStreamReader(new FileInputStream(args[0]), "UTF-8"));
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }

        /**1.获取配置参数**/
        //#基准表对应查询字段放入到数组中，例如：filedArr1=names,hobby
        String filed1 = properties.get("filedArr1").toString();
        String[] filedArr1 = filed1.split(",");

        //#目标表对应查询字段放入到数组中 例如：filedArr2=names,hobby
        String filed2 = properties.get("filedArr2").toString();
        String[] filedArr2 = filed2.split(",");

        //在循环比对时，由于SQL模板中，读取索引位置从1开始，所以调整目标表的字段索引
        String[] updateFileArr2 = new String[filedArr2.length + 1];
        for (int i = 0; i < filedArr2.length; i++) {
            updateFileArr2[i + 1] = filedArr2[i];
        }

        GeneratorConfig generatorConfig = new GeneratorConfig(Integer.valueOf(properties.get("dataType").toString()),
                properties.get("templateCode").toString(),
                properties.get("tableName1").toString(),
                properties.get("tableName2").toString(), filedArr1, updateFileArr2,
                properties.get("province").toString());

        /**2.查询SQL模板**/
        dataProcessUtils = new DataProcessUtils();
        List<Map<String, Object>> mdmTemplateList = dataProcessUtils.selectSqlTemplate(generatorConfig);

        /**3.动态获取SQL**/
        if (mdmTemplateList.size() > 0) {
            dataProcessUtils.getTemplateSql(mdmTemplateList, generatorConfig);
        } else {
            System.out.println("未查询到符合条件的模板");
        }
    }
}
